package com.hex.liyue.lookingdatemodule.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hex.liyue.R;
import com.hex.liyue.commonmodule.activity.BaseActivity;
import com.hex.liyue.commonmodule.utils.DateUtils;
import com.hex.liyue.commonmodule.utils.L;
import com.hex.liyue.commonmodule.utils.SexUtils;
import com.hex.liyue.commonmodule.utils.StatusBarCompatUtils;
import com.hex.liyue.commonmodule.view.recycleview.YRecycleview;
import com.hex.liyue.commonmodule.view.scrollbanner.BannerViewPager;
import com.hex.liyue.commonmodule.view.scrollbanner.ViewFactory;
import com.hex.liyue.lookingdatemodule.adapter.AdapterCommissioned;
import com.hex.liyue.lookingdatemodule.bean.AppointmentDetailsResultBean;
import com.hex.liyue.lookingdatemodule.presenter.IPersonDetailsView;
import com.hex.liyue.lookingdatemodule.presenter.PersonDetailsPresenter;
import com.hex.liyue.storemodule.adapter.AdapterStore;
import com.hex.liyue.usermodule.bean.MyMessageResultBean;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by $cmm on 2016/7/4.
 * 寻找约会中应约榜列表的点击事件
 */
public class CommissionedDetailsActivity extends BaseActivity implements IPersonDetailsView {

    @Bind(R.id.iv_toobar_back)
    ImageView ivToobarBack;
    @Bind(R.id.tv_toobar_title)
    TextView tvToobarTitle;
    @Bind(R.id.bn_person_banner)
    BannerViewPager bnPersonBanner;

    @Bind(R.id.tv_desc)
    TextView tvDesc;
    @Bind(R.id.tv_name)
    TextView tvName;
    @Bind(R.id.tv_sex_age)
    TextView tvSexAge;
    @Bind(R.id.rv_gifts)
    YRecycleview rvGifts;
    @Bind(R.id.tv_giftlists)
    TextView tvGiftlists;
    @Bind(R.id.iv_avater)
    ImageView ivAvater;


    private List<ImageView> views;//图片组件
    private List<String> infos;//图片URL地址
     private List<Integer> info;//图片URL地址
    private String uid;//用户ID
    private PersonDetailsPresenter personDetailsPresenter;
    private List<AppointmentDetailsResultBean.DataBean.ApplyListBean.GiftListBean> lists;
    private AdapterCommissioned adapterCommissioned;
    private AppointmentDetailsResultBean.DataBean.ApplyListBean giftListBean;

    @Override
    protected void bindPresenter() {
        personDetailsPresenter = new PersonDetailsPresenter(this);
    }

    @Override
    protected void initData(Bundle extras) {
        lists = new ArrayList<>();
        uid = getIntent().getExtras().getString("uid");
        giftListBean = (AppointmentDetailsResultBean.DataBean.ApplyListBean) getIntent().getExtras().getSerializable("gift_list");
//        if (giftListBean != null) {
//            lists.addAll((Collection<? extends AppointmentDetailsResultBean.DataBean.ApplyListBean.GiftListBean>) giftListBean);
//        }

        personDetailsPresenter.getProfile(uid);
        // 在加载数据前设置是否循环
        infos = new ArrayList<>();
        views = new ArrayList<>();
        info=new ArrayList<>();
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_details_commissioned;
    }

    @Override
    protected void initToolbar() {
        StatusBarCompatUtils.compat(this);
        ivToobarBack.setVisibility(View.VISIBLE);
    }

    @Override
    public void setPersonDetails(MyMessageResultBean myMessageResultBean) {
        tvToobarTitle.setText(myMessageResultBean.data.nickname);
        tvName.setText(myMessageResultBean.data.nickname);
        if(!"".equals(myMessageResultBean.data.signature)){
            tvDesc.setText(myMessageResultBean.data.signature);
        }else{
            tvDesc.setText("这个人太懒啦，什么都没有说");
        }
        tvSexAge.setText(SexUtils.getSex(myMessageResultBean.data.sex) + "   " + DateUtils.getAge(myMessageResultBean.data.birthday * 1000) + "岁    " + myMessageResultBean.data.star_sign);
        if (giftListBean.gift_list.size() <= 0) {
            tvGiftlists.setVisibility(View.GONE);
        } else {
            if (adapterCommissioned == null) {
                adapterCommissioned = new AdapterCommissioned();
                adapterCommissioned.setList(giftListBean.gift_list);
                rvGifts.setLayoutManager(new LinearLayoutManager(this));
                rvGifts.setReFreshEnabled(false);
                rvGifts.setLoadMoreEnabled(false);
                rvGifts.setAdapter(adapterCommissioned);
            }
            adapterCommissioned.setList(giftListBean.gift_list);
            adapterCommissioned.notifyDataSetChanged();
            rvGifts.reSetStatus();
        }
        if (myMessageResultBean.data.urls.size()>1) {
            for (MyMessageResultBean.DataBean.UrlsBean urlsBean : myMessageResultBean.data.urls) {
                infos.add(urlsBean.url);
            }
            L.d("WJ", "size=" + infos.toString());
            if (infos.size() >= 1) {
                // 将第一个ImageView添加进来
                views.add(ViewFactory.getImageView(this, infos.get(0)));
                if (infos.size() > 1) {
                    // 将最后一个ImageView添加进来
                    views.add(ViewFactory.getImageView(this, infos.get(infos.size() - 1)));
                    for (int i = 0; i < infos.size(); i++) {
                        views.add(ViewFactory.getImageView(this, infos.get(i)));
                    }
                }
                bnPersonBanner.setData(views, infos, null);
                //开始轮播,必须调用这个方法,不然不会自动轮播
                if (views.size() == 1) {
                    bnPersonBanner.setWheel(false);
                } else {
                    bnPersonBanner.setWheel(true);
                }
                // 设置轮播时间，默认3000ms
                bnPersonBanner.setScrollTime(2000);
                //设置圆点指示图标组居中显示，默认靠右
                bnPersonBanner.setIndicatorCenter();
            }
        }else if(myMessageResultBean.data.urls.size()==1){
            bnPersonBanner.setVisibility(View.GONE);
            ivAvater.setVisibility(View.VISIBLE);
            Glide.with(this).load(myMessageResultBean.data.urls.get(0).url).centerCrop().error(R.drawable.default_error_image).into(ivAvater);
        }else{
            bnPersonBanner.setVisibility(View.GONE);
            ivAvater.setVisibility(View.VISIBLE);
            Glide.with(this).load(myMessageResultBean.data.avatar).centerCrop().error(R.drawable.default_error_image).into(ivAvater);
        }
    }

    @OnClick({R.id.iv_toobar_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_toobar_back:
                onBackPressed();
                break;
        }
    }

    @Override
    public void showLod(String msg) {
        showLoad(this, msg);
    }

    @Override
    public void closeLod() {
        closeLoad();
    }

    @Override
    public void showT(String msg) {
        showToast(this, msg);
    }

}
